home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C13 / NoMemory.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  739 b   |  32 lines

  1. //: C13:NoMemory.cpp
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. // Constructor isn't called
  7. // If new returns 0
  8. #include <iostream>
  9. #include <new> // size_t definition
  10. using namespace std;
  11.  
  12. void my_new_handler() {
  13.   cout << "new handler called" << endl;
  14. }
  15.  
  16. class NoMemory {
  17. public:
  18.   NoMemory() {
  19.     cout << "NoMemory::NoMemory()" << endl;
  20.   }
  21.   void* operator new(size_t sz) throw(bad_alloc){
  22.     cout << "NoMemory::operator new" << endl;
  23.     throw bad_alloc(); // "Out of memory"
  24.   }
  25. };
  26.  
  27. int main() {
  28.   set_new_handler(my_new_handler);
  29.   NoMemory* nm = new NoMemory;
  30.   cout << "nm = " << nm << endl;
  31. } ///:~
  32.